package com.tanhua.dubbo.api.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itheima.dubbo.api.UserApi;
import com.tanhua.dubbo.mapper.UserInfoMapper;
import com.tanhua.dubbo.mapper.UserMapper;
import com.tanhua.model.domain.User;
import com.tanhua.model.domain.UserInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @Author Pan Rundong
 * @Date 2023-02-24 20:14
 * @Desc 用户API实现类
 */
@DubboService(version = "1.0.0")
public class UserApiImpl implements UserApi {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 根据用户手机查询用户
     * @Author: Pan Rundong
     * @Date: 2023-02-24 20:15
     * @Greet2u:  （O｀ 3′O）~~♡
     * @param phone
     * @return com.tanhua.model.domain.User
     */
    @Override
    public User findPhone(String phone) {
        //1.设置条件，以手机号为条件查询用户表
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getMobile, phone);
        //2.查询
        User user = userMapper.selectOne(queryWrapper);
        //3.返回
        return user;
    }

    /**
     * 保存用户，并返回id
     * @Author: Pan Rundong
     * @Date: 2023-02-24 20:15
     * @Greet2u:  （O｀ 3′O）~~♡
     * @param user
     * @return java.lang.Long
     */
    @Override
    public Long save(User user) {
        //1.保存用户
        userMapper.insert(user);
        //2.查询用户id
        return user.getId();
    }

    /**
     * 保存用户Info
     * @Author: Pan Rundong
     * @param userInfo
     * @return void
     */
    @Override
    public void saveUser(UserInfo userInfo) {
        //保存
        userInfoMapper.insert(userInfo);
    }

    /**
     * 更新用户信息
     *
     * @param user
     * @return void
     * @Author Pan Rundong
     */
    @Override
    public void update(User user) {
        userMapper.updateById(user);
    }

    /**
     * 查询全部info
     *
     * @param
     * @return java.util.List<com.tanhua.model.domain.User>
     * @Author Pan Rundong
     */
    @Override
    public List<User> findAll() {
        return userMapper.selectList(Wrappers.emptyWrapper());
    }

    /**
     * 根据UserId查询
     *
     * @param userId
     * @return com.tanhua.model.domain.User
     * @Author Pan Rundong
     */
    @Override
    public User findById(Long userId) {
        return userMapper.selectById(userId);
    }

    /**
     * 根据环信Id获取用户信息
     *
     * @param huanxinId
     * @return com.tanhua.model.domain.User
     * @Author Pan Rundong
     */
    @Override
    public User findByHuanXinId(String huanxinId) {
        //1.设置条件
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getHxUser, huanxinId);
        //2.查询并返回
        return userMapper.selectOne(queryWrapper);
    }
}
